package srx.open.com.utils;

import android.content.Context;

import java.sql.Time;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import srx.open.com.R;

/**
 *  日期处理.
 */
public class DateUtils {

    public static String daysBetween(Context context, String createDate) {
        try {
            // 创建的日期.
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Calendar cal = Calendar.getInstance();
            cal.setTime(sdf.parse(createDate));
            long time1 = cal.getTimeInMillis();
            return TimeUtil.getDescriptionTimeFromTimestamp(time1);
            // 现在的日期.
//            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
//            String bdate = df.format(new Date());
//            cal.setTime(sdf.parse(bdate));
//            long time2 = cal.getTimeInMillis();
//            long betweenTime=(time2-time1);
//            return dateToTime(context, betweenTime) + "";
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    private final static long HOUR = (3600); // 时
    private final static long DAY = (3600 * 24); // 天

    public static String dateToTime(Context context, long time) {
        try {
            time = time / 1000;
            long day = (time / DAY);
            if (day != 0) {

                String dayBeforeStr = context.getResources().getString(R.string.day_before_str).toString();
//                return day + "天以前";
                return day + dayBeforeStr;
            }
            // 换算时.
            long hour = time / HOUR;
            if (hour != 0) {
                String hourBeforeStr = context.getResources().getString(R.string.hour_before_str).toString();
//                return hour + "小时以前";
                return hour + hourBeforeStr;
            }
            long min = time % HOUR / 60;
            if (min != 0) {
                String minrBeforeStr = context.getResources().getString(R.string.min_before_str).toString();
//                return min + "分钟以前";
                return min + minrBeforeStr;
            }
            long sec = time % HOUR % 60;
            if (sec != 0) {
                String secBeforeStr = context.getResources().getString(R.string.sec_before_str).toString();
//                return sec + "秒以前";
                return sec + secBeforeStr;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        String justBeginningStr = context.getResources().getString(R.string.just_beginning_str).toString();
//        return "刚刚";
        return justBeginningStr;
    }

}
